package com.kgc.mall.mapper;


import com.kgc.mall.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {

   /**
    * 按多个条件分页查询用户信息列表
    * @param startNo 页码
    * @param pageSize 页大小
    * @param loginName 登录名
    * @param realName 真实姓名
    * @return 返回用户实体对象集合
    */
   @Select("<script>SELECT * from `easybuy_user` "
           + "WHERE 1=1 "
           + "<if test='loginName != null and loginName.trim().length() >0'>"
           + " AND loginName=#{loginName}"
           + "</if>"
           + "<if test='realName != null and realName.trim().length() >0'>"
           + " AND userName like CONCAT('%',#{realName},'%')"
           + "</if>"
           + " ORDER BY id ASC "
           + " LIMIT #{startNo},#{pageSize} "
           + "</script>")
   List<User> getUsersByPaging(
           @Param("startNo") int startNo,
           @Param("pageSize") int pageSize,
           @Param("loginName") String loginName,
           @Param("realName") String realName);

   /**
    * 按多个条件查询用户总记录数
    * @param loginName
    * @param realName
    * @return
    */
   @Select("<script>select count(0) from `easybuy_user`" +
     "WHERE 1=1"+
     "<if test='loginName != null and loginName.trim().length()>0'>"+
     "AND loginName=#{loginName}"+
     "</if>"+
     "<if test='realName !=null and realName.trim().length()>0'>"+
     "AND userName like CONCAT('%',#{realName},'%')"+
     "</if>"+
     "</script>")
   long getUserTotalCount(@Param("loginName") String loginName,
                          @Param("realName") String realName);

   /**
    * 根据用户id获得用户详细信息
    * @param id 用户id
    * @return
    */
   @Select("<script>SELECT * from `easybuy_user` WHERE id = #{id}</script>")
   User getUser(int id);

   /**
    * 通过用户id列表获得用户信息列表(Excel导出功能)
    * @param userIds 用户id列表
    * @return
    */
   List<User> getUserListByIds(List<Integer> userIds);

   /**
    * 根据用户id修改指定用户信息
    * @param user 用户实体对象
    * @return
    */
   @Update("<script>UPDATE `easybuy_user` set mobile = #{mobile}, " +
     "email = #{email}, " +
     "type = #{type} "+
     "WHERE id = #{id}" +
     "</script>")
   int updateUser(User user);

   /**
    * 根据用户id删除指定用户信息
    * @param id
    * @return
    */
   @Delete("<script>DELETE from `easybuy_user` WHERE id = #{id}</script>")
   int deleteUser(int id);

   /**
    * 添加用户信息
    * @param user
    * @return
    */
   @Insert("<script>INSERT INTO `easybuy_user` values" +
     "(null,#{loginName},#{userName},#{password},#{sex},#{identityCode},#{email},#{mobile},#{type}); </script>")
   int insertUser(User user);

   /**
    * 批量更新用户信息
    * @param userList
    * @return
    */
   @Update({
     "<script>",
     "<foreach collection='list' item='item' index='index' separator=';'>",
     "update  `easybuy_user` ",
     "set loginName= #{item.loginName} , userName= #{item.userName} ," ,
     " password=#{item.password} , sex=#{item.sex} ,identityCode=#{item.identityCode} ,email=#{item.email} ," ,
     "mobile=#{item.mobile} ,type=#{item.type} where id= #{item.id} ",
     "</foreach>",
     "</script>"
   })
   int updateBatch(List<User> userList);

   /**
    * 批量逻辑删除用户信息
    * @param userIds
    * @return
    */
   @Delete("<script>DELETE from `easybuy_user` WHERE id in " +
     "<foreach collection='userIds' item='item' index='index' separator=','>" +
     "(#{item})</foreach></script>")
   int deleteBatch(List<Integer> userIds);

   /**
    * 批量添加用户信息
    * @param userList
    * @return
    */
   @Insert("<script>" +
     "INSERT INTO `easybuy_user`(loginName, userName, password, sex,identityCode,email,mobile,type) values " +
     "<foreach collection='list' item='item' index='index' separator=','>" +
     "( #{item.loginName},#{item.userName},#{item.password},#{item.sex},#{item.identityCode},#{item.email},#{item.mobile},#{item.type})" +
     "</foreach></script>")
   int insertBatch(List<User> userList);
}
